package top.hkyzf.study.leetcode.array;

import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 朱峰
 * @date 2022-1-29 10:24
 */
public class 组合_77 {

    public List<List<Integer>> combine(int n, int k) {
        LinkedList<Integer> trace = new LinkedList<>();
        List<List<Integer>> list = new ArrayList<>();
        dfs(n, k, 1, trace, list);
        return list;
    }

    void dfs(int n, int k, int begin, LinkedList<Integer> trace, List<List<Integer>> list) {
        if (k == trace.size()) {
            list.add(new LinkedList<>(trace));
            return;
        }
        for (int i = begin; i <= n; i++) {
            trace.add(i);
            dfs(n, k, i+1, trace, list);
            trace.removeLast();
        }
    }

    @Test
    public void testCombine () {
        int n = 4;
        int k = 2;
        System.out.println(combine(n, k));
    }
}
